<template>
  <div>
    <!-- 面包屑 -->
    <app-bread>{{ $route.meta.title }}</app-bread>

    <!-- 查询功能 -->
    <el-form class="search" :inline="true" :model="searchForm">
      <el-form-item>
        <el-input
          v-model="searchForm.name"
          placeholder="用户名"
          style="width:160px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.sex"
          placeholder="性别"
          style="width:80px"
        >
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary">重置</el-button>
        <el-button type="success" @click="dialogFormVisible = true"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 用户列表数据 -->
    <el-table :data="tableData" border style="width: 100%" stripe>
      <el-table-column prop="name" label="姓名" width="100"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="80"> </el-table-column>
      <el-table-column prop="phone" label="手机号码" width="100">
      </el-table-column>
      <el-table-column prop="birthday" label="出生日期" width="100">
      </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <!-- <h1>{{ scope.row.pic }}</h1> -->
          <img :src="scope.row.pic" style="width:50px" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template>
          <el-button type="success">编辑</el-button>
          <el-button type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
    >
    </el-pagination>

    <!-- 新增和编辑 -->
    <el-dialog title="新增" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="iw">
          <el-input
            v-model="form.name"
            autocomplete="off"
            style="width:200px"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" label-width="100px">
          <el-select v-model="form.sex" placeholder="性别" style="width:80px">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机" :label-width="iw">
          <el-input
            v-model="form.phone"
            autocomplete="off"
            style="width:200px"
            placeholder="请输入手机"
          ></el-input>
        </el-form-item>
        <el-form-item label="出生日期" :label-width="iw">
          <el-date-picker
            v-model="form.birthday"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="地址" :label-width="iw">
          <el-input
            v-model="form.address"
            autocomplete="off"
            style="width:200px"
            placeholder="请输入地址"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="iw">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload></el-form-item
        >
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import appBread from "../../components/Breadcrumb";
export default {
  data() {
    return {
      searchForm: {
        name: "",
        sex: ""
      },
      tableData: [
        {
          _id: "5f1e997342d02334e8d65041",
          name: "赵四222",
          sex: "女",
          phone: "16644449999",
          birthday: "2020-08-05",
          address: "斗罗大陆",
          fileNames: "webp (1).webp",
          pic:
            "http://120.76.247.5:2002/uploads/8a26b5c39ac406eb730e051667833451-1602405012017.jpg",
          username: "null"
        },
        {
          _id: "5f1e9dca42d02334e8d65047",
          name: "特朗普",
          sex: "阴阳人",
          phone: "16011331133",
          birthday: "2020-07-23",
          address: "白宫",
          fileNames: "webp (3).webp",
          pic: "",
          username: "undefined"
        },
        {
          _id: "5f1f83092e1ea833083376a1",
          name: "仝卓",
          sex: "阴阳人",
          phone: "13689798945",
          birthday: "2020-07-02",
          address: "北京",
          fileNames: "webp (1).webp",
          pic: "",
          username: "undefined"
        }
      ],
      currentPage4: 4,
      dialogFormVisible: false, //弹窗的开关
      form: {
        //新增用户数据
        name: "",
        sex: "",
        phone: "",
        birthday: "",
        address: ""
      },
      iw: "100px",
      fileList: [
        {
          name: "food.jpeg",
          url:
            "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
        },
        {
          name: "food2.jpeg",
          url:
            "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100"
        }
      ]
    };
  },

  components: {
    appBread
  },

  methods: {
    onSubmit() {
      console.log("submit!");
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    }
  },

  created() {
    console.log("我进来了");
  },

  //路由钩子：组件内
  beforeRouteLeave(to, from, next) {
    window.console.log("离开user组件了");
    next();
  }
};
</script>

<style scoped>
.search {
  margin-top: 20px;
}
</style>